this.name = "Oolite Shipyard Extension - hOopy Casino";
this.author = "Paul Wilkins";
this.copyright = "(C) 2009 Paul Wilkins";
this.description = "Converted from the 0.69 version of OSE-XML.plist";
this.version = "0.70";

this.startUp = function () {
    if (oolite.compareVersion("1.73.4") > 0)
    {
        // Oolite version is older than 1.74.
        log("script." + this.name, "This hOopy Casino script does not run under Oolite version " + oolite.versionString + ". hOopy Casino is disabled.");
        delete this.shipWillExitWitchspace;
        delete this.shipWillLaunchFromStation;
        delete this.shipLaunchedFromStation;
        delete this.shipWillDockWithStation;
        delete this.shipDockedWithStation;
		delete this.guiScreenChanged;
    }
    this.cleanup = this.CLEANUP;
};
this.reset = function () {
    this.cleanup = this.CLEANUP;
};
this.shipWillLaunchFromStation = function (station) {
    this.cleanupStation();
};
this.DISABLE = function (name) {
    if (!this.old) {
		this.old = {};
	}
    if (!name || name === '') {
		return;
	}
    if (!this.old[name]) {
		this.old[name] = this[name];
	}
    delete this[name];
};
this.ENABLE = function (name) {
    if (!name || name === '' || !(this.old)) {
		return;
	}
    this[name] = this.old[name];
};
this.CLEANUP = function () { //can't check missionVariables inside startUp or reset
    if (system.techLevel <= 9) // No hOopy casino available
    {
        //no need to run any of these!
        this.DISABLE("shipWillExitWitchspace");
        this.DISABLE("shipWillLaunchFromStation");
        this.DISABLE("shipDockedWithStation");	
        this.DISABLE("guiScreenChanged");
        this.DISABLE("shipLaunchedFromStation");
    } 
    else
    {
        if (this.old)
        {
            this.ENABLE("shipWillExitWitchspace");	
            this.ENABLE("shipWillLaunchFromStation");
            this.ENABLE("shipDockedWithStation");
            this.ENABLE("guiScreenChanged");		
            this.ENABLE("shipLaunchedFromStation");		
            log("script.hoopy-casino", "Re-enabling hOopy casino.");
        }
    }
    this.cleanup = function () {}; // run just once per script startup/reset
};

var ANARCHY = 0,
	FEUDAL = 1,
    MULTIGOVERNMENT = 2,
    DICTATORSHIP = 3,
    COMMUNIST = 4,
	CONFEDERACY = 5,
	DEMOCRACY = 6,
	CORPORATESTATE = 7;
var RICHINDUSTIAL = 0,
	AVERAGEINDUSTRIAL = 1,
	POORINDUSTRIAL = 2,
	MAINLYINDUSTRIAL = 3,
	MAINLYAGRICULTURAL = 4,
	RICHAGRICULTURAL = 5,
	AVERAGEAGRICULTURAL = 6,
	POORAGRICULTURAL = 7;

this.shipWillExitWitchspace = this.shipWillLaunchFromStation = function () {
	if (system.techLevel >= 10 && system.countShipsWithPrimaryRole('casinoship') === 0) {
		system.legacy_addSystemShips('casinoship', 1, 1.0);
	}
};
this.shipDockedWithStation = function (station) {
	if (station.name === 'CoachWhip hOopy Casino') {
		if (!missionVariables.hoopy_casino || missionVariables.hoopy_casino === 'HOOPY_REVISIT') {
            if (player.credits >= 100) {
                mission.runMissionScreen('hoopy_casino_intro', 'hoopy_front.png', 'hoopy_casino_enter_yesno', null, 'hoopy_theme.ogg');
                missionVariables.hoopy_casino = 'HOOPY_ENTER';
            }
		}
    }
};
this.guiScreenChanged = function (to, from) {
    if (missionVariables.hoopy_casino === 'HOOPY_ENTER') {
        if (mission.choice === 'YES') {
            mission.runMissionScreen('hoopy_casino_desc', 'hoopy_clean.png', 'hoopy_casino_gamble_yesno');
            missionVariables.hoopy_casino = 'HOOPY_GAMBLE';
        } else if (mission.choice === 'NO') {
            missionVariables.hoopy_casino = 'NOT_NOW';
        }
    }
    if (missionVariables.hoopy_casino === 'HOOPY_GAMBLE') {
        if (mission.choice === '0') {
            mission.runMissionScreen('hoopy_casino_desc', 'hoopy_clean.png', 'hoopy_casino_gamble_yesno');
        } else if (mission.choice === 'YES') {
            missionVariables.hoopy_casino = 'HOOPY_GAMEON';
        } else if (mission.choice === 'NO') {
            missionVariables.hoopy_casino = 'NOT_NOW';
        }
    }
    if (missionVariables.hoopy_casino === 'HOOPY_GAMEON') {
        mission.runMissionScreen('hoopy_casino_choices', 'hoopy_cover.png', 'hoopy_casino_pick_hoop');
        this.hoopy_casino_hoop = Math.random() * 3;
        missionVariables.hoopy_casino = 'HOOPY_MAKE_CHOICE';
    }
    if (missionVariables.hoopy_casino === 'HOOPY_MAKE_CHOICE') {
        if (mission.choice !== null && mission.choice !== 'None') {
            missionVariables.hoopy_casino = 'HOOPY_CHOSEN';
        }
    }
    if (missionVariables.hoopy_casino === 'HOOPY_CHOSEN') {
        missionVariables.hoopy_casino = 'HOOPY_LOSER';
        if (this.hoopy_casino_hoop < 1) {
			this.hoopy_casino_image = 'hoopy_Lwin.png';
            mission.setBackgroundImage(missionVariables.hoopy_casino_image);
            if (mission.choice === 'LEFT') {
                missionVariables.hoopy_casino = 'HOOPY_WINNER';
            }
        }
        if (this.hoopy_casino_hoop >= 1 && this.hoopy_casino_hoop < 2) {
			this.hoopy_casino_image = 'hoopy_Cwin.png';
            mission.setBackgroundImage(missionVariables.hoopy_casino_image);
            if (mission.choice === 'MIDDLE') {
                missionVariables.hoopy_casino = 'HOOPY_WINNER';
            }
        }
        if (this.hoopy_casino_hoop >= 2) {
			this.hoopy_casino_image = 'hoopy_Rwin.png';
            mission.setBackgroundImage(missionVariables.hoopy_casino_image);
            if (mission.choice === 'RIGHT') {
                missionVariables.hoopy_casino = 'HOOPY_WINNER';
            }
        }
    }
    if (missionVariables.hoopy_casino === 'HOOPY_WINNER') {
        mission.runMissionScreen('hoopy_casino_win', this.hoopy_casino_image, 'hoopy_casino_replay_winner_yesno');
        player.credits += 100;
        missionVariables.hoopy_casino = 'HOOPY_REPLAY';
    }
    if (missionVariables.hoopy_casino === 'HOOPY_LOSER') {
        mission.runMissionScreen('hoopy_casino_loss', this.hoopy_casino_image, 'hoopy_casino_replay_loser_yesno');
        player.credits -= 100;
        missionVariables.hoopy_casino = 'HOOPY_REPLAY';
    }
    if (missionVariables.hoopy_casino === 'HOOPY_REPLAY') {
        if (mission.choice === '0-WINNER') {
            mission.runMissionScreen('hoopy_casino_win', this.hoopy_casino_image, 'hoopy_casino_replay_winner_yesno');
        }
        if (mission.choice === '0-LOSER') {
			mission.runMissionScreen('hoopy_casino_loss', this.hoopy_casino_image, 'hoopy_casino_replay_loser_yesno');
        }
        if (mission.choice === 'YES') {
            if (player.credits >= 100) {
                mission.runMissionScreen('hoopy_casino_again', 'hoopy_clean.png', '');
                missionVariables.hoopy_casino = 'HOOPY_GAMEON';
            } else {
                mission.runMissionScreen('hoopy_casino_nofunds', 'hoopy_front.png', '');
                missionVariables.hoopy_casino = 'NOT_NOW';
            }
        }
        if (mission.choice === 'NO') {
            mission.runMissionScreen('hoopy_casino_goodbye', 'hoopy_front.png', '');
            missionVariables.hoopy_casino = 'NOT_NOW';
        }
	}
	
};
this.shipLaunchedFromStation = function () {
	if (missionVariables.hoopy_casino > '') {
		missionVariables.hoopy_casino = 'HOOPY_REVISIT';
	}
	delete this.hoopy_casino_hoop;
	delete this.hoopy_casino_image;
};
